package com.leesche.zcsp.node.web.service.impl;

import com.leesche.zcsp.node.app.v1.vo.DeviceInfoVo;
import com.leesche.zcsp.node.app.v1.vo.FactoryDeviceVo;
import com.leesche.zcsp.node.net.service.NetService;
import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.web.dao.PubDeviceDao;
import com.leesche.zcsp.node.web.model.PubDevice;
import com.leesche.zcsp.node.web.model.PubDeviceCriteria;
import com.leesche.zcsp.node.web.service.PubDevUpdateFirmWareService;
import com.leesche.zcsp.node.web.service.PubDeviceService;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import com.leesche.zcsp.node.web.vo.v2.FactoryDeviceListVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author dennies yang
 * @version V1.0
 * @Title: PubDeviceServiceImpl.java
 * @Package com.leesche.zcsp.node.service.impl
 * @Description:()
 * @date 2016年9月22日 下午4:21:55
 */
@Service
@Transactional
public class PubDeviceServiceImpl extends
                                  BaseServiceImpl<PubDevice, PubDeviceCriteria, PubDeviceDto, QueryPubDeviceCondition, Long> implements
                                                                                                                             PubDeviceService {
    @Autowired
    private PubDeviceDao pubDeviceDao;
    @Autowired
    private PubDevUpdateFirmWareService firmWareService;
    @Autowired
    private NetService netService;

    @Autowired
    public void setBaseDao(PubDeviceDao pubDeviceDao) {
        super.setBaseDao(pubDeviceDao);
    }

    /* (non-Javadoc)
     * @see com.leesche.zcsp.node.web.service.PubDeviceService#getDeviceIDByShortCode(java.lang.String)
     */
    @Override
    public Integer getDeviceIDByShortCode(String shortCode) {
        // TODO Auto-generated method stub
        return pubDeviceDao.selectByShortCode(shortCode);
    }

    @Override
    public List<InfoDeviceDto> findDeviceList(QueryPubDeviceCondition condition) {
        return pubDeviceDao.findDeviceList(condition);
    }

    @Override
    public InfoDeviceDto findInfoDevice(QueryPubDeviceCondition condition) {
        return pubDeviceDao.findInfoDevice(condition);
    }

    @Override
    public List<Map<String, Object>> getDeviceListsByUserid(Long userid) {
        return pubDeviceDao.selectByUserID(userid);
    }

    /* (non-Javadoc)
     * @see com.leesche.zcsp.node.web.service.PubDeviceService#editPubDeviceByImme(com.leesche.zcsp.node.web.model
     * .PubDevice)
     */
    @Override
    public Integer editPubDeviceByImme(PubDevice pubDevice) {
        return pubDeviceDao.updateByImme(pubDevice);
    }

    @Override
    public PubDeviceDto selectByCondition(QueryPubDeviceCondition condition) {
        return pubDeviceDao.selectByCondition(condition);
    }

    @Override
    public List<FactoryDeviceVo> selectFactoryDeviceVo(QueryPubDeviceCondition condition) {
        return pubDeviceDao.selectFactoryDeviceList(condition);
    }

    @Override
    public PagingResult<FactoryDeviceVo> selectFactoryDeviceListByPage(QueryPubDeviceCondition condition) {
        return new PagingResult<FactoryDeviceVo>(pubDeviceDao.selectFactoryDeviceListByPage(condition), pubDeviceDao
                .selectFactoryDeviceListByPageCount(condition), condition.getPage());
    }

    @Override
    public FactoryDeviceVo selectFactoryDevice(QueryPubDeviceCondition condition) {
        return pubDeviceDao.selectFactoryDevice(condition);
    }

    @Override
    public PagingResult<FactoryDeviceListVo> selectFactoryDeviceListVo(QueryPubDeviceCondition condition) {
        return new PagingResult<FactoryDeviceListVo>(pubDeviceDao
                .selectFactoryDeviceListVoByPage(condition), pubDeviceDao
                .selectFactoryDeviceListVoByPageCount(condition), condition.getPage());
    }

    @Override
    public List<DeviceCountDto> countDeviceForFactory(QueryDeviceCountCondition condition) {
        return pubDeviceDao.countDeviceForFactory(condition);
    }

    @Override
    public List<DeviceCountDataDto> findDeviceDataForFactory(QueryDeviceCountCondition condition) {
        return pubDeviceDao.selectDeviceDataForFactory(condition);
    }

    @Override
    public int updateVersionByImme(Map map) {
        return pubDeviceDao.updateVersionByImme(map);
    }

    @Override
    public PagingResult<InfoDeviceDto> selectDeviceListByPage(QueryPubDeviceCondition condition) {
        return new PagingResult<InfoDeviceDto>(pubDeviceDao
                .findDeviceListByPage(condition), pubDeviceDao
                .findDeviceListByPageCount(condition), condition.getPage());
    }

    @Override
    public PubDevice checkFactoryDevice(QueryPubDeviceCondition condition) {
        return pubDeviceDao.checkFactoryDevice(condition);
    }

    @Override
    public DeviceInfoVo findDeviceInfo(QueryPubDeviceCondition condition) {
        InfoDeviceDto dto = pubDeviceDao.findInfoDevice(condition);
        String imme = dto.getImme();
        boolean flag = netService.isOnline(imme);
        if(flag){
            dto.setIsOnline(1);
        }else{
            dto.setIsOnline(2);
        }
        QueryPubDevUpdateFirmWareCondition firmWareCondition = new QueryPubDevUpdateFirmWareCondition();
        firmWareCondition.setImei(imme);
        List<PubDevUpdateFirmWareDto> deviceFirmWares = firmWareService.findDeviceFirmWares(firmWareCondition);
        DeviceInfoVo vo = new DeviceInfoVo();
        vo.setDto(dto);
        vo.setDeviceFirmWares(deviceFirmWares);
        return vo;
    }
}
